package com.itheima.sc.impl.sms;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.sc.api.sms.SmsCouponApi;
import com.itheima.sc.mapper.sms.SmsCouponMapper;
import com.itheima.sc.pojo.sms.Coupon;
import com.itheima.sc.pojo.sms.HomeBrand;
import com.itheima.sc.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class SmsCouponImpl implements SmsCouponApi {

    @Autowired
    private SmsCouponMapper couponMapper;

    //添加优惠券
    public Long addCoupon(Coupon coupon) {
         couponMapper.insert(coupon);
        return coupon.getId();
    }

    //删除优惠券
    public void deleteCouponById(Long id) {
        couponMapper.deleteById(id);
    }

    //根据优惠券名称和类型分页获取优惠券列表
    public PageResult selectCouponList(Integer page, Integer pageSize, Integer type, String name) {
        //构建分页查询条件
        Page<Coupon> ipage = new Page<>(page, pageSize);
        //构建条件查询
        LambdaQueryWrapper<Coupon> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(type != null, Coupon::getType, type);
        wrapper.like(StrUtil.isNotBlank(name), Coupon::getName, name);

        //查询请求
        ipage = couponMapper.selectPage(ipage, wrapper);
        return new PageResult(page, pageSize, Convert.toInt(ipage.getTotal()),
                Convert.toInt(ipage.getTotal() / pageSize), ipage.getRecords());
    }

    //修改优惠券
    public void updateCoupon(Coupon coupon) {
        couponMapper.updateById(coupon);
    }

    //查询优惠券详情
    public Coupon selectCouponById(Long id) {
        return couponMapper.selectById(id);
    }
}
